package com.example.maybeaccount.Mode;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/** 账单类，存放账单数据 */
public class Item implements Serializable {
    private double amount = 0;   //金额
    private String class1;   //一级分类
    private String class2;   //二级分类
    private String account;  //账户 与 信贷平台复用
    private String member;   //成员
    private String shop;     //商户
    private String project;  //项目
    private String remark;   //备注
    private Calendar cal = Calendar.getInstance() ;
    private Calendar calPayBack =Calendar.getInstance() ;
    private double income = 0;
    private double expend = 0;
    private int id;          //在数据库中的id
    private int mode;
    public static final int MODE_INCOME = 0;
    public static final int MODE_OUTCOME = 1;
    public static final int MODE_CREDIT = 2;
    public static final int MODE_CREDIT_PAYBACK = 3;

    /**
     * 自动设置时间为系统时间
     */
    public String setTimeAuto(){
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(cal.getTime());
    }

    /**
     * 从输入获取时间 (各个时间分开输入)
     */
    public void setTimeManual(int year, int month, int day, int hour ,int minute){
        cal.set(year, month-1, day, hour, minute);
    }

    /**
     * 从输入获取时间（输入时间为字符串）
     */
    public void setTimeByString(String stringTime){
        //将字符串转化为Date，添加至Calendar
        Date date = StringDateUtils.cutDate(stringTime);
        cal = Calendar.getInstance();
        cal.setTime(date);

    }

    /**
     * 从输入获取还款时间（输入时间为字符串）
     */
    public void setPayBackTimeByString(String stringTime){
        //将字符串转化为Date，添加至Calendar
        Date date = StringDateUtils.cutDate(stringTime);
        calPayBack = Calendar.getInstance();
        calPayBack.setTime(date);

    }

    /**
     * 属性存取
     */
    public void updateAmount(double change){
        amount += change;
        if(change >= 0){
            income += change;
        }else{
            expend += Math.abs(change);
        }
    }

    /**
     * 根据前端记账返回的MAP设置item的属性值
     * @param dataset
     * @param mode
     */
    public void initItemByMap( Map<String,String> dataset,int mode ){
        if(dataset.size() != 0){
            switch (mode){
                case MODE_OUTCOME:
                    this.amount = (-1) * Math.abs(Double.parseDouble(dataset.get("amount")));
                    this.class1 = dataset.get("lv1_option");
                    this.class2 = dataset.get("lv2_option");
                    this.account = dataset.get("account");
                    this.member = dataset.get("member");
                    this.shop = dataset.get("business");
                    this.project = dataset.get("project");
                    this.remark = dataset.get("remark");
                    this.mode = mode;
                    this.setTimeByString(dataset.get("time"));
                    break;

                case MODE_INCOME:
                    this.amount = Math.abs(Double.parseDouble(dataset.get("amount")));
                    this.class1 = dataset.get("lv1_option");
                    this.account = dataset.get("account");
                    this.remark = dataset.get("remark");
                    this.mode = mode;
                    this.setTimeByString(dataset.get("time"));
                    this.shop = dataset.get("business");
                    this.project = dataset.get("project");
                    break;

                case MODE_CREDIT:
                    this.amount = (-1) * Math.abs(Double.parseDouble(dataset.get("amount")));
                    this.class1 = dataset.get("lv1_option");
                    this.class2 = dataset.get("lv2_option");
                    this.account = dataset.get("platform");
                    this.member = dataset.get("member");
                    this.shop = dataset.get("business");
                    this.project = dataset.get("project");
                    this.remark = dataset.get("remark");
                    this.mode = mode;
                    this.setTimeByString(dataset.get("time"));
                    this.setPayBackTimeByString(dataset.get("repaytime"));
                    break;

                default:
            }
        }
    }

    public void setCal(Calendar cal){
        this.cal = cal;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public void setClass1(String class1) {
        this.class1 = class1;
    }

    public void setClass2(String class2) {
        this.class2 = class2;
    }

    public void setMember(String member) {
        this.member = member;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public void setShop(String shop) {
        this.shop = shop;
    }

    public void setExpend(double expend) {
        this.expend = expend;
    }

    public void setIncome(double income) {
        this.income = income;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getYear(){
        return cal.get(Calendar.YEAR);
    }

    public int getMonth() {
        int month = cal.get(Calendar.MONTH);
        return cal.get(Calendar.MONTH)+1;
    }

    public int getDay() {
        return cal.get(Calendar.DATE);
    }

    public int getHour() {
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    public int getMinute() {
        return cal.get(Calendar.MINUTE);
    }

    public long getTimeMill(){
        return cal.getTimeInMillis();
    }

    public double getAmount() {
        return amount;
    }

    public String getAccount() {
        return account;
    }

    public String getClass1() {
        return class1;
    }

    public String getClass2() {
        return class2;
    }

    public String getMember() {
        return member;
    }

    public String getRemark() {
        return remark;
    }

    public String getProject() {
        return project;
    }

    public String getShop() {
        return shop;
    }

    public Calendar getSetUpCalendar(){
        return this.cal;
    }

    public Calendar getPayBackCalendar(){
        return this.calPayBack;
    }

    public String getStringTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(cal.getTime());
    }

    public String getStringPayBackTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(calPayBack.getTime());
    }

    public String getStringPayBackTimeWithoutYear(){
        SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");
        return df.format(calPayBack.getTime());
    }

    public String getStringPayBackYear(){
        return calPayBack.get(Calendar.YEAR)+"";
    }


    public String getStringMonth(){
        return this.getMonth() + "";
    }

    public String getStringDay(){
        return this.getDay() + "";
    }

    public String getStringYear(){
        return this.getYear() + "";
    }

    public String getStringIncome(){
        return income + "";
    }

    public String getStringExpend(){
        return expend + "";
    }

    public double getIncome() {
        return income;
    }

    public double getExpend() {
        return expend;
    }

    public Calendar getCal() {
        return cal;
    }

    public int getId() {
        return id;
    }

    public int getMode() {
        return mode;
    }


}
